<template>
  <div class="mod-shopInfo-add-or-update">
    <div class="title">{{$t("shop.myShop")}}</div>
    <div id="shopForm">
      <el-form
        :model="dataForm"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="120px"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item :label="this.$i18n.t('shop.storeName')" prop="shopName">
              <el-input v-model="dataForm.shopName" maxlength="20" show-word-limit></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item :label="this.$i18n.t('shop.storeStatus')" prop="shopStatus">
              <el-tag v-if="dataForm.shopStatus == -1">{{$t("shop.nonactivated")}}</el-tag>
              <el-tag v-else-if="dataForm.shopStatus == 0" type="danger">{{$t("shop.closed")}}</el-tag>
              <el-tag v-else-if="dataForm.shopStatus == 1" type="success">{{$t("shop.inOperation")}}</el-tag>
              <el-tag v-else-if="dataForm.shopStatus == 2" type="danger">{{$t("groups.offlineViolation")}}</el-tag>
              <el-tag v-else-if="dataForm.shopStatus == 3" type="warning">{{$t("productComm.platformAudit")}}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item :label="this.$i18n.t('shop.bindMobilePhoneNumber')" prop="mobile">
              <el-input v-model="dataForm.mobile"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="this.$i18n.t('shop.contactTel')" prop="tel">
              <el-input v-model="dataForm.tel"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item :label="this.$i18n.t('address.province')" prop="province">
              <el-select v-model="dataForm.provinceId" :placeholder="this.$i18n.t('tip.select')" @change="selectProvince">
                <el-option
                  v-for="province in provinceList"
                  :key="province.areaId"
                  :label="province.areaName"
                  :value="province.areaId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="city" :label="this.$i18n.t('address.city')">
              <el-select v-model="dataForm.cityId" :placeholder="this.$i18n.t('tip.select')" @change="selectCity">
                <el-option
                  v-for="city in cityList"
                  :key="city.areaId"
                  :label="city.areaName"
                  :value="city.areaId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="area" :label="this.$i18n.t('temp.area')">
              <el-select v-model="dataForm.areaId" :placeholder="this.$i18n.t('tip.select')" @change="mapLocation(true)">
                <el-option
                  v-for="area in areaList"
                  :key="area.areaId"
                  :label="area.areaName"
                  :value="area.areaId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item :label="this.$i18n.t('shop.shopAddress')" prop="shopAddress">
          <el-input v-model="dataForm.shopAddress" @change="mapLocation">
            <el-button slot="append" icon="el-icon-location" @click="mapLocation">{{$t("admin.location")}}</el-button>
          </el-input>
        </el-form-item>

        <el-form-item>
          <baidu-map
            class="map"
            :scroll-wheel-zoom="false"
            :center="center"
            @moving="syncCenterAndZoom"
            @moveend="syncCenterAndZoom"
            @zoomend="syncCenterAndZoom"
            :zoom="zoom"
          >
            <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
            <bm-marker :position="center" :dragging="false" @click="infoWindowOpen">
              <bm-info-window :show="show" @close="infoWindowClose" @open="infoWindowOpen">{{$t("shop.storeLocation")}}</bm-info-window>
            </bm-marker>
          </baidu-map>
        </el-form-item>

        <el-form-item :label="this.$i18n.t('shop.shopDescription')" prop="intro">
          <el-input type="textarea" :rows="2" v-model="dataForm.intro"></el-input>
        </el-form-item>

        <el-row>
          <el-col :span="8">
            <el-form-item :label="this.$i18n.t('shop.shopLogo')" prop="shopLogo">
              <img-upload v-model="dataForm.shopLogo" class="upload-logo"></img-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item :label="this.$i18n.t('shop.businessLicense')" prop="businessLicense">
              <pic-upload v-model="dataForm.businessLicense" :disabled="true" class="license"></pic-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8" class="idcard-width">
            <el-form-item :label="this.$i18n.t('shop.frontOfIDCard')" prop="identityCardFront">
              <pic-upload v-model="dataForm.identityCardFront" :disabled="true" class="idcard"></pic-upload>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="idcard-width">
            <el-form-item :label="this.$i18n.t('shop.idCardReverse')" prop="identityCardLater">
              <pic-upload v-model="dataForm.identityCardLater" :disabled="true" class="idcard"></pic-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row style="margin-top: 30px;">
          <el-form-item>
            <!-- <el-button @click="resetForm()">重置</el-button> -->
            <el-button type="primary" @click="dataFormSubmit()">{{$t("shop.submitChanges")}}</el-button>
            <!-- <el-button type="primary"
                       icon="el-icon-document-checked"
                       v-if="dataForm.shopStatus > 1"
            @click="auditEventHandle()">下线管理</el-button>-->
          </el-form-item>
        </el-row>
      </el-form>
    </div>

    <!-- 下线管理弹窗-->
    <offlineEventHandle
      v-if="offlineEventHandleVisible"
      selectUrl="/shop/shopDetail/getOfflineHandleEventByShopId"
      applyUrl="/shop/shopDetail/auditApply"
      ref="offlineEvent"
      @refreshDataList="refreshChange"
    ></offlineEventHandle>
  </div>
</template>

<script>
import PicUpload from '@/components/pic-upload'
import ImgUpload from '@/components/img-upload'
import OfflineEventHandle from '@/components/offline-event-handle'
export default {
  data () {
    return {
      provinceList: [],
      cityList: [],
      areaList: [],
      isEditAddr: false,
      dataForm: {
        shopId: '',
        shopStatus: '',
        shopName: '',
        shopOwner: '',
        mobile: '',
        tel: '',
        shopLat: '',
        shopLng: '',
        province: '',
        city: '',
        area: '',
        shopAddress: '',
        intro: '',
        shopLogo: '',
        identityCardFront: '',
        identityCardLater: '',
        businessLicense: '',
        areaId: null,
        cityId: null,
        provinceId: null
      },
      show: false,
      center: {
        lng: 113.327955,
        lat: 23.136783
      },
      zoom: 18,
      offlineEventHandleVisible: false
    }
  },
  components: {
    ImgUpload,
    PicUpload,
    OfflineEventHandle
  },
  created: function () {
    this.init()
  },
  methods: {
    init () {
      this.visible = true
      this.show = false
      this.setCenter()
      this.getDataInfo()
    },
    // 获取店铺详情数据
    getDataInfo () {
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.$refs.dataForm.resetFields()
        this.cityList = []
        this.areaList = []
        this.dataForm.provinceId = null
        this.dataForm.cityId = null
        this.dataForm.areaId = null
      })
      this.listAreaByParentId().then(({ data }) => {
        this.provinceList = data
      })
      this.$http({
        url: this.$http.adornUrl('/shop/shopDetail/info'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.dataForm.shopId = data.shopId
        this.dataForm.shopStatus = data.shopStatus
        this.dataForm.shopName = data.shopName
        this.dataForm.shopOwner = data.shopOwner
        this.dataForm.mobile = data.mobile
        this.dataForm.tel = data.tel
        this.dataForm.shopLat = data.shopLat
        this.dataForm.shopLng = data.shopLng
        this.dataForm.shopAddress = data.shopAddress
        this.dataForm.intro = data.intro
        this.dataForm.shopLogo = data.shopLogo
        this.dataForm.identityCardFront = data.identityCardFront
        this.dataForm.identityCardLater = data.identityCardLater
        this.dataForm.businessLicense = data.businessLicense
        this.dataForm.areaId = data.areaId
        this.dataForm.cityId = data.cityId
        this.dataForm.provinceId = data.provinceId
        this.listAreaByParentId(data.provinceId).then(({ data }) => {
          this.cityList = data
        })
        this.listAreaByParentId(data.cityId).then(({ data }) => {
          this.areaList = data
          this.center = {
            lng: this.dataForm.shopLng,
            lat: this.dataForm.shopLat
          }
        })
      })
    },
    listAreaByParentId (pid) {
      if (!pid) pid = 0
      return this.$http({
        url: this.$http.adornUrl(`/admin/area/listByPid`),
        method: 'get',
        params: this.$http.adornParams({ pid })
      })
    },
    // 选择省
    selectProvince (val) {
      this.dataForm.cityId = null
      this.dataForm.city = ''
      // 获取城市的select
      this.listAreaByParentId(val).then(({ data }) => {
        this.cityList = data
      })
      this.mapLocation(true)
    },
    // 选择市
    selectCity (val) {
      this.dataForm.areaId = null
      this.dataForm.area = ''
      // 获取区的select
      this.listAreaByParentId(val).then(({ data }) => {
        this.areaList = data
      })
      this.mapLocation(true)
    },
    // 重置表单
    // resetForm () {
    //   this.$refs.dataForm.resetFields()
    // },
    // 表单提交
    dataFormSubmit () {
      if (this.isEditAddr) {
        this.$message.info(this.$i18n.t('admin.mapPosi'))
        return
      }
      this.getAreaName()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/shop/shopDetail'),
            method: 'put',
            data: this.$http.adornData({
              shopName: this.dataForm.shopName,
              shopOwner: this.dataForm.shopOwner,
              mobile: this.dataForm.mobile,
              tel: this.dataForm.tel,
              shopLat: this.center.lat,
              shopLng: this.center.lng,
              area: this.dataForm.area,
              city: this.dataForm.city,
              province: this.dataForm.province,
              shopAddress: this.dataForm.shopAddress,
              intro: this.dataForm.intro,
              shopLogo: this.dataForm.shopLogo,
              identityCardFront: this.dataForm.identityCardFront,
              identityCardLater: this.dataForm.identityCardLater,
              businessLicense: this.dataForm.businessLicense,
              areaId: this.dataForm.areaId,
              cityId: this.dataForm.cityId,
              provinceId: this.dataForm.provinceId
            })
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList', this.page)
              }
            })
          })
        }
      })
    },
    /**
     * 初始化地图数据
     */
    setCenter () {
      this.center = null
      this.center = {
        lng: 113.327955,
        lat: 23.136783
      }
      this.zoom = 18
    },
    /**
     * 获取地图移动后回调的位置参数
     */
    syncCenterAndZoom (e) {
      this.setCenter()
      const { lng, lat } = e.target.getCenter()
      this.center.lng = lng
      this.center.lat = lat
      this.zoom = e.target.getZoom()
      this.isEditAddr = false
    },
    /**
     * 通过省市区id,得到省市区名称
     */
    getAreaName () {
      for (let i = 0; i < this.provinceList.length; i++) {
        if (this.provinceList[i].areaId === this.dataForm.provinceId) {
          // 将省名字保存起来
          this.dataForm.province = this.provinceList[i].areaName
        }
      }
      for (let i = 0; i < this.cityList.length; i++) {
        if (this.cityList[i].areaId === this.dataForm.cityId) {
          // 将市名字保存起来
          this.dataForm.city = this.cityList[i].areaName
        }
      }
      for (let i = 0; i < this.areaList.length; i++) {
        if (this.areaList[i].areaId === this.dataForm.areaId) {
          // 将市名字保存起来
          this.dataForm.area = this.areaList[i].areaName
        }
      }
    },
    // 定位地图
    mapLocation (isTrue) {
      let area = ''
      this.getAreaName()
      if (isTrue === true) {
        // 选择省市区时，定位地图
        if (this.dataForm.provinceId) {
          area = this.dataForm.province
          this.zoom = 6
          if (this.dataForm.cityId) {
            area = area + this.dataForm.city
            this.zoom = 10
            if (this.dataForm.areaId) {
              area = area + this.dataForm.area
              this.zoom = 14
              if (this.dataForm.addr !== null) {
                // 详细地址
                area = this.dataForm.province + this.dataForm.city + this.dataForm.area + this.dataForm.addr
                this.zoom = 18
              }
            }
          }
        }
      } else {
        this.center = null
        if (!this.dataForm.provinceId || !this.dataForm.cityId || !this.dataForm.areaId) {
          this.dataForm.shopAddress = null
          this.errorMsg(this.$i18n.t('admin.selePCD'))
          return
        }
        area = this.dataForm.province + this.dataForm.city + this.dataForm.area + this.dataForm.shopAddress
        this.zoom = 18
      }
      this.center = area
      this.isEditAddr = true
    },
    /**
     * 关闭地图说明标签
     */
    infoWindowClose () {
      this.show = false
    },
    /**
     * 打开地图说明标签
     */
    infoWindowOpen () {
      this.show = true
    },
    // 下线管理
    auditEventHandle () {
      this.offlineEventHandleVisible = true
      this.$nextTick(() => {
        this.$refs.offlineEvent.init(this.dataForm.shopId)
      })
    },
    // 刷新数据
    refreshChange () {
      this.getDataInfo()
    }
  }
}
</script>
<style lang="scss" >
.mod-shopInfo-add-or-update {
  margin: 0px auto;

  #shopForm {
    //border:1px blue solid;
    width: 1000px;
    margin: 0px auto;
  }

  .title {
    margin-bottom: 20px;
    font-size: 20px;
    text-align: center;
  }

  .shop {
    width: 500px;
  }

  // .upload-logo .pic-uploader-component,
  // .upload-logo .pic-uploader-component > .el-upload,
  // .upload-logo .pic-uploader-component > .el-upload > img {
  //   width: 300px !important;
  //   height: 100px !important;
  // }

  .idcard-width {
    width: 40% !important;
  }
  .idcard .pic-uploader-component,
  .idcard .pic-uploader-component .el-upload--text > img {
    width: 260px !important;
    height: 150px !important;
  }

  .license,
  .license .pic-uploader-component .el-upload > img {
    height: 220px !important;
  }
}
.map {
  width: 100%;
  height: 500px;
}
</style>
